Rock Animation

The goal for Rock Animation was when the laser from the ship hits a large asteroid, it would release a random element, and break into two medium asteroids. When a laser hits a medium asteroid, it would release another random element and break into two small asteroids. A laser hit on a small asteroid, would release another random element. Therefore, one large asteroid will release seven random elements if fully mined. Les has designed an explosion, with sound, on a laser and asteroid hit, and will be incorporated with my animation in the final project. Below is a screenshot of the division of asteroids.
AsteroidBreakup.PNG
Asteroid Breakup

As you can see, the far left asteroid has been broken down into four small asteroids. The middle asteroid has been reduced to two small asteroids and one medium. The right asteroid (at the bottom about to leave the screen) hasn't been hit with a laser. The position of each is marked with a random element release.

Code

The code for detecting a collision between laser & asteroid and releasing a random element has not change from Project 1 and can be viewed in the Project 1 Report here . The major change to the code was the addition of more asteroids when breaking into medium and small asteroids. This was incorporated in the CreateChildAsteroid method.


private AsteroidObject CreateChildAsteroid(AsteroidObject parentAsteroid, AsteroidState state)
{
    AsteroidObject newAsteroid = null;
    if(state == AsteroidState.Medium)
    {
    newAsteroid = new AsteroidObject(GetRandomMediumAsteroidContent(), state);
    newAsteroid.IsAlive = true;
    newAsteroid.Position = new Vector2(parentAsteroid.XPos + 100, parentAsteroid.YPos + (float)(random.NextDouble() * 50));
    newAsteroid.Velocity = parentAsteroid.Velocity;
    }
    if (state == AsteroidState.Small)
    {
    newAsteroid = new AsteroidObject(GetRandomSmallAsteroidContent(), state);
    newAsteroid.IsAlive = true;
    newAsteroid.Position = new Vector2(parentAsteroid.XPos + 35, parentAsteroid.YPos + (float)(random.NextDouble() * 50));
    newAsteroid.Velocity = parentAsteroid.Velocity;
    }
    return newAsteroid;
}



This method creates a new asteroid object based on the parent asteroid that was just destroyed and the desired state of the new asteroid. In order to prevent the asteroids from being on top of each other, a random buffer is added to each coordinate position. This method only creates one asteroid, the other asteroid that makes the new pair is given the same properties as the parent except a change in the AsteroidState. An example of how both are handled is displayed below. This excerpt is taken from the UpdateLasers method and is triggered on a collision between laser and asteroid.

foreach (AsteroidObject asteroid in asteroids)
{
    Rectangle asteroidRect = new Rectangle(
        (int)asteroid.XPos,
        (int)asteroid.YPos,
        asteroid.Sprite.Width,
        asteroid.Sprite.Height);
    if (laserRect.Intersects(asteroidRect))
    {
        laser.IsAlive = false;
        //edit here to change states
        if (asteroid.AsteroidState == AsteroidState.Large)
        {
        asteroid.Sprite = GetRandomMediumAsteroidContent();
        asteroid.AsteroidState = AsteroidState.Medium;
        asteroids.Add(CreateChildAsteroid(asteroid, AsteroidState.Medium));
        ElementObject element = GetRandomElement();
        element.Position = asteroid.Position;
        element.IsAlive = true;
        elements.Add(element);
        break;
        }
        if (asteroid.AsteroidState == AsteroidState.Medium)
        {
        asteroid.Sprite = GetRandomSmallAsteroidContent();
        asteroid.AsteroidState = AsteroidState.Small;
        asteroids.Add(CreateChildAsteroid(asteroid, AsteroidState.Small));
        ElementObject element = GetRandomElement();
        element.Position = asteroid.Position;
        element.IsAlive = true;
        elements.Add(element);
        break;
        }
        if (asteroid.AsteroidState == AsteroidState.Small)
        {
        asteroid.IsAlive =false;
        asteroids.Remove(asteroid);
        ElementObject element = GetRandomElement();
        element.Position = asteroid.Position;
        element.IsAlive = true;
        elements.Add(element);
        asteroid.IsAlive = false;
        break;
        }
 
    }
}